<?php
    /**
     * 楼盘基本信息上传
     */
namespace app\manage\controller;
    use \think\Validate;
class Building extends \app\common\controller\Manage
{
    //插入楼盘基本信息
    public function inserts(){
        $data = input('param.');
        cookie('lala',$data);
        $rule = [
            'name'  => 'require|unique:building',
            'company'   => 'require',
            'description'   => 'require',
            'country' => 'require',
            'slogan' => 'require',
            'design_img' => 'require',
            'share_tit' => 'require',
            'share_desc' => 'require',
            'size' => 'require',
            'price' => 'require',
            'address' => 'require',
        ];
        $msg = [
            'name.require' => '楼盘名不能为空',
            'name.unique' => '已录入该楼盘',
            'description.unique' => '楼盘描述不能为空',
            'country.require' => '所属国家不能为空',
            'company.require' => '公司不能为空',
            'slogan.require' => '楼盘口号不能为空',
            'design_img.require' => '效果图不能为空',
            'share_tit.require' => '分享信息不能为空',
            'share_desc.require' => '分享描述不能为空',
            'size.require' => '面积不能为空',
            'price.require' => '价格不能为空',
            'address.require' => '地址不能为空',
        ];
        $validate   = Validate::make($rule,$msg);
        $result = $validate->check($data);

        if(!$result)
            return [
                'message' => $validate->getError()
            ];
        if(isset($data['id'])){
            //更新数据
            $saveData['name'] = trim(strip_tags($data['name']));
            $saveData['company'] = trim(strip_tags($data['company']));
            $saveData['description'] = trim(strip_tags($data['description']));
            $saveData['country'] = trim(strip_tags($data['country']));
            $saveData['slogan'] = trim(strip_tags($data['slogan']));
            $saveData['design_img'] = $data['design_img'];
            $saveData['back_img'] = $data['back_img'];
            $saveData['sharetitle'] = trim(strip_tags($data['share_tit']));
            $saveData['sharedec'] = trim(strip_tags($data['share_desc']));
            $saveData['size'] = trim(strip_tags($data['size']));
            $saveData['price'] = trim(strip_tags($data['price']));
            $saveData['address'] = trim(strip_tags($data['address']));
            $saveData['date'] = time();
            \app\common\model\Building::where('id',$data['id']) -> update($saveData);
            return [
                'code' => '2',
                'message' => '更新成功',
                'id' => $data['id'],
            ];
        }else{
            //新增数据
            $saveData['name'] = trim(strip_tags($data['name']));
            $saveData['company'] = trim(strip_tags($data['company']));
            $saveData['description'] = trim(strip_tags($data['description']));
            $saveData['country'] = trim(strip_tags($data['country']));
            $saveData['slogan'] = trim(strip_tags($data['slogan']));
            $saveData['design_img'] = $data['design_img'];
            $saveData['back_img'] = $data['back_img'];
            $saveData['sharetitle'] = trim(strip_tags($data['share_tit']));
            $saveData['sharedec'] = trim(strip_tags($data['share_desc']));
            $saveData['size'] = trim(strip_tags($data['size']));
            $saveData['price'] = trim(strip_tags($data['price']));
            $saveData['address'] = trim(strip_tags($data['address']));
            $saveData['date'] = time();
            $building = \app\common\model\Building::create($saveData);
            return [
                'code' => '1',
                'message' => '新增成功',
                'id' => $building->id
            ];
        }
    }
    //删除当前信息
    public function dels(){
        $result = \app\common\model\Building::get(input('id'));
        \app\common\model\Building::destroy(input('id'));
        $map['id'] = array('eq',input('id'));
        \app\common\model\Building::where($map) -> delete();
        //删除当前他下面的所有小模块
        $map1['building_id'] = array('eq',input('id'));
        \app\common\model\BuildingDesc::where($map1) -> delete();
        //删除当前他的角色排序
        $list = $result['list'];
        $map2['list'] = array('eq',$list);
        $result1 = \app\common\model\BuildingList::where($map2) -> find();
        $list5 = $result1['building_id'];
        $lis = explode(',',$list5);
        $new = '';
        foreach ($lis as $value){
            if($value !== input('id')){
                $new = $new.','.$value;
            }
        }
        $list1 = ltrim($new,',');
        \app\common\model\BuildingList::where($map2) -> setField('building_id',$list1);
        return [
            'code' => '1',
            'message' => '删除成功'
        ];
    }
    //取消优选楼盘
    public function delYx(){
        $data = input('param.');
        $map['id'] = array('eq',$data['id']);
        $result = \app\common\model\Building::where($map) -> find();
        if($data['sign']){
            \app\common\model\Building::where($map) -> setField('list_enterlo',0);
            $list = $result['list_enterlo'];
        }else{
            \app\common\model\Building::where($map) -> setField('list',0);
            $list = $result['list'];

        }
        $map1['list'] = array('eq',$list);
        if($data['sign']){
            $result1 = \app\common\model\BuildingListEnterlo::where($map1) -> find();
        }else{
            $result1 = \app\common\model\BuildingList::where($map1) -> find();
        }
        $list5 = $result1['building_id'];
        $lis = explode(',',$list5);
        $new = '';
        foreach ($lis as $value){
            if($value !== $data['id']){
                $new = $new.','.$value;
            }
        }
        $list1 = ltrim($new,',');
        if($data['sign']){
            \app\common\model\BuildingListEnterlo::where($map1) -> setField('building_id',$list1);
        }else{
            \app\common\model\BuildingList::where($map1) -> setField('building_id',$list1);
        }
        return [
            'code' => '1',
            'message' => '取消成功'
        ];
    }
    //添加至优选楼盘
    public function addYx(){
        $data = input('param.');
        $map['id'] = array('eq',$data['id']);
        $wxw =  \app\common\model\Building::where($map) -> find();
        switch ($data['type']){
            case 'yx':\app\common\model\Building::where($map) -> setField('list',1);
                        $map1['list'] = array('eq',1);break;
            case 'gd':\app\common\model\Building::where($map) -> setField('list',2);
                        $map1['list'] = array('eq',2);break;
            case 'sy':\app\common\model\Building::where($map) -> setField('list',3);
                        $map1['list'] = array('eq',3);break;
            case 'hw':\app\common\model\Building::where($map) -> setField('list',4);
                        $map1['list'] = array('eq',4);break;
            case 'wl':\app\common\model\Building::where($map) -> setField('list',5);
                        $map1['list'] = array('eq',5);break;
            case 'gn':\app\common\model\Building::where($map) -> setField('list_enterlo',1);
                        $map1['list'] = array('eq',1);break;
            case 'gw':\app\common\model\Building::where($map) -> setField('list_enterlo',2);
                        $map1['list'] = array('eq',2);break;
            default: return [
                'code' => '0',
                'message' => '请选择类别'
            ];
        }
        if($data['sign']){
            $wxw1 =  \app\common\model\Building::where($map) -> find();
            $lis = $wxw['list_enterlo'];
            $lis1 = $wxw1['list_enterlo'];
            if($lis!==0 && $lis !== $lis1){
                $mapw['list'] = array('eq',$lis);
                $result5 = \app\common\model\BuildingListEnterlo::where($mapw) -> find();
                if($result5){
                    $listw = strval($result5['building_id']);
                    $res = strpos($listw,$data['id']);
                    if($res || $res == 0){
                        $rewx = '';
                        $rew = explode(',',$listw);
                        foreach ($rew as $value){
                            if($value != $data['id']){
                                $rewx = ltrim($rewx.','.$value);
                            }
                        }
                        $rex = ltrim($rewx,',');
                        \app\common\model\BuildingListEnterlo::where($mapw) -> setField('building_id',$rex);
                    }
                }
            }
            $result = \app\common\model\BuildingListEnterlo::where($map1) -> find();
            if($result){
                $list = $result['building_id'];
                $array5 = explode(',',$list);
                if(!in_array($data['id'],$array5)){
                    $resu = $list.','.$data['id'];
                    $resul = ltrim($resu,',');
                    $map2['id'] = array('eq',$result['id']);
                    \app\common\model\BuildingListEnterlo::where($map2) -> setField('building_id',$resul);
                    return [
                        'code' => '1',
                        'message' => '添加成功'
                    ];
                }else{
                    return [
                        'code' => '0',
                        'message' => '已添加'
                    ];
                }
            }else{
                switch ($data['type']){
                    case 'gn':$savedata['list'] = 1;break;
                    case 'gw':$savedata['list'] = 2;break;default;
                }
                $savedata['building_id'] = $data['id'];
                \app\common\model\BuildingListEnterlo::create($savedata);
                return [
                    'code' => '1',
                    'message' => '添加成功'
                ];
            }
        }else{
            $wxw1 =  \app\common\model\Building::where($map) -> find();
            $lis = $wxw['list'];
            $lis1 = $wxw1['list'];
            if($lis!==0 && $lis !== $lis1){
                $mapw['list'] = array('eq',$lis);
                $result5 = \app\common\model\BuildingList::where($mapw) -> find();
                if($result5){
                    $listw = strval($result5['building_id']);
                    $res = strpos($listw,$data['id']);
                    if($res || $res == 0){
                        $rewx = '';
                        $rew = explode(',',$listw);
                        foreach ($rew as $value){
                            if($value != $data['id']){
                                $rewx = ltrim($rewx.','.$value);
                            }
                        }
                        $rex = ltrim($rewx,',');
                        \app\common\model\BuildingList::where($mapw) -> setField('building_id',$rex);
                    }
                }
            }
            $result = \app\common\model\BuildingList::where($map1) -> find();
            if($result){
                $list = $result['building_id'];
                $array6 = explode(',',$list);
                if(!in_array($data['id'],$array6)){
                    $resu = $list.','.$data['id'];
                    $resul = ltrim($resu,',');
                    $map2['id'] = array('eq',$result['id']);
                    \app\common\model\BuildingList::where($map2) -> setField('building_id',$resul);
                    return [
                        'code' => '1',
                        'message' => '添加成功'
                    ];
                }else{
                    return [
                        'code' => '0',
                        'message' => '已添加'
                    ];
                }
            }else{
                switch ($data['type']){
                    case 'yx':$savedata['list'] = 1;break;
                    case 'gd':$savedata['list'] = 2;break;
                    case 'sy':$savedata['list'] = 3;break;
                    case 'hw':$savedata['list'] = 4;break;
                    case 'wl':$savedata['list'] = 5;break;default;
                }
                $savedata['building_id'] = $data['id'];
                \app\common\model\BuildingList::create($savedata);
                return [
                    'code' => '1',
                    'message' => '添加成功'
                ];
            }
        }
    }
}
    
